#include <stdio.h>  // stdin, stdout
#include <string.h>  // strlen
#include <stdlib.h>  // malloc

#include "tnet.h"  // for Buffer, chunk, etc
#include "pgi.h"

// 
// DUMMY handlers 
// Eventually these will be generated.
// And we will import dispatch.gen.h too, and pass that to pgi_serve().
//

typedef struct EchoRequest_s {
  char* name;
  int id;
} EchoRequest;

typedef struct EchoResponse_s {
  char* name;
  int id;
} EchoResponse;


typedef struct MyAppContext_s {
  int base;
} MyAppContext;


int EchoHandler(void* app_ctx, EchoRequest* request, EchoResponse* response) {
  MyAppContext* my_app_ctx = (MyAppContext*)app_ctx;
  response->id = request->id + my_app_ctx->base +  5;
  return 0;
}

// this one is generated.
//
// response_buf needs to be filled.  call tnet_free_buffer afterward?
// change to char* route
//
// #include handlers.gen.h
//
// if (strcmp(route, "Echo") == 0) {
//   // deserialize
//   call echo
//   // serialize
// }

int my_dispatch(
    void* app_ctx,
    const char* route,
    const char* in_data, size_t in_length,
    char** out_data, size_t* out_length) {

  // Expected to malloc
  *out_data = (char*)malloc(10);
  strcpy(*out_data, "foo\n");
  *out_length = strlen(*out_data);
  return 1;
}


int main(int argc, char **argv)
{
  MyAppContext* app_ctx = NULL;

  // invoke_handlers should be passed here
  pgi_serve(&my_dispatch, (void*)app_ctx);

  return 0;  // never reached
}
